package com.fanyouzhao.ehome.web.servlet; /**
 * @Author: fanyouzhao
 * @Date: 11:03 2021/5/10
 */

import com.alibaba.fastjson.JSON;
import com.fanyouzhao.ehome.entity.Cp;
import com.fanyouzhao.ehome.service.CpService;
import com.fanyouzhao.ehome.service.impl.CpServiceImpl;
import com.fanyouzhao.ehome.util.JsonResult;
import com.fanyouzhao.ehome.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "CouponServlet", value = "/coupon")
public class CouponServlet extends HttpServlet {

    private final CpService cpService = new CpServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //中文处理
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        ((HttpServletResponse)response).setHeader("content-Type","text/html;charset=UTF-8");

        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(null);

        String method = request.getHeader("method");
        switch (method){
            case "selectByPage":{
                Integer page = Integer.parseInt(request.getParameter("page"));
                Integer limit = Integer.parseInt(request.getParameter("limit"));
                try {
                    List<Cp> cps = selectByPage(page, limit);
                    if (cps != null) {
                        jsonResult = new JsonResult("0","查询成功",cps);
                        jsonResult.setValue("count",getCount());
                    } else {
                        jsonResult = new JsonResult("1","查询失败！");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
            case "addCp":{
                String formData = request.getParameter("formData");
                Cp cp = JSON.parseObject(formData, Cp.class);
                try {
                    Boolean aBoolean = addCp(cp);
                    if (aBoolean) {
                        jsonResult = new JsonResult("1","新建成功");
                    } else {
                        jsonResult = new JsonResult("0","新建失败");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
            case "updateCp":{
                Integer id = Integer.parseInt(request.getParameter("id"));
                String formData = request.getParameter("formData");
                Cp cp = JSON.parseObject(formData, Cp.class);
                try {
                    Boolean update = update(id, cp);
                    if (update) {
                        jsonResult = new JsonResult("1","修改成功");
                    } else {
                        jsonResult = new JsonResult("0","修改失败");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
            case "delCp":{
                Integer id = Integer.parseInt(request.getParameter("id"));
                try {
                    Boolean aBoolean = deleteById(id);
                    if (aBoolean) {
                        jsonResult = new JsonResult("1","删除成功");
                    } else {
                        jsonResult = new JsonResult("0","删除失败");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
            case "off":{
                Integer id = Integer.parseInt(request.getParameter("id"));
                try {
                    Cp cp = selectById(id);
                    cp.setInUse(false);
                    Boolean update = update(id, cp);
                    if (update) {
                        jsonResult = new JsonResult("1","下架成功");
                    } else {
                        jsonResult = new JsonResult("0","下架失败");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
            case "on":{
                Integer id = Integer.parseInt(request.getParameter("id"));
                try {
                    Cp cp = selectById(id);
                    cp.setInUse(true);
                    Boolean update = update(id, cp);
                    if (update) {
                        jsonResult = new JsonResult("1","上架成功");
                    } else {
                        jsonResult = new JsonResult("0","上架失败");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
            case "getStatus":{
                Integer id = Integer.parseInt(request.getParameter("id"));
                try {
                    Cp cp = selectById(id);
                    if (cp!=null){
                        boolean inUse = cp.isInUse();
                        if (inUse){
                            jsonResult.setCode("1");
                        }else{
                            jsonResult.setCode("0");
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }

            default:
                break;
        }

        ResponseUtil.send(response,JSON.toJSONString(jsonResult.getValues()));

    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    public List<Cp> selectByPage(Integer page, Integer limit) throws Exception {
        return cpService.selectByPage(page, limit);
    }
    public Boolean update(Integer id, Cp cp) throws Exception {
        return cpService.update(id,cp);
    }
    public Cp selectById(Integer id) throws Exception {
        return cpService.selectById(id);
    }
    public Boolean deleteById(Integer id) throws Exception {
        return cpService.deleteById(id);
    }
    public Boolean addCp(Cp cp) throws Exception {
        return cpService.insert(cp);
    }
    public Integer getCount() throws Exception{
        return cpService.getCount();
    }
    public List<Cp> selectAll() throws Exception{
        return cpService.selectAll();
    }

}
